
#include "Enemy.h"


/////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------- CLASS ENEMY -------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////////////////

Enemy::Enemy( int spawn_point_x, int spawn_point_y ) : Character()
{
    character_image.load( "grafx/enemy1.gif" );

    health = 400;
    damage = 20;

    //DRAW_X = spawn_x;
    spawn_x = spawn_point_x;

    distance_to_enemy = 230;
    enemy_speed = 3;
    frame_left = 0;
    frame_right = 0;
    death_frame_x = 390;
    death_frame_y = 0;

    following_player = false;
    bullet_fire_rate = 0;

    animation_speed_left = 0;
    animation_speed_right = 0;

    sprite_info.w = 63;
    sprite_info.h = 74;
    sprite_info.y = spawn_point_y;
    enemy_current_floor_y = spawn_point_y;

    on_block = false;

}

Enemy::Enemy( const Enemy &new_obj )
{
    character_image = new_obj.character_image;

    health = new_obj.health;
    damage = new_obj.damage;

    spawn_x = new_obj.spawn_x;

    distance_to_enemy = new_obj.distance_to_enemy;
    enemy_speed = new_obj.enemy_speed;
    frame_left = new_obj.frame_left;
    frame_right = new_obj.frame_right;
    death_frame_x = new_obj.death_frame_x;
    death_frame_y = new_obj.death_frame_y;

    following_player = new_obj.following_player;
    bullet_fire_rate = new_obj.bullet_fire_rate;

    animation_speed_left = new_obj.animation_speed_left;
    animation_speed_right = new_obj.animation_speed_right;

    sprite_info.w = new_obj.sprite_info.w;
    sprite_info.h = new_obj.sprite_info.h;
    sprite_info.y = new_obj.sprite_info.y;
    enemy_current_floor_y = new_obj.enemy_current_floor_y;

    on_block = new_obj.on_block;
}

Enemy::~Enemy()
{

}

void Enemy::Set_enemy( int spawn_point_x, int spawn_point_y )
{
    character_image.load( "grafx/enemy1.gif" );

    health = 400;
    damage = 20;

    //DRAW_X = spawn_x;
    spawn_x = spawn_point_x;

    distance_to_enemy = 230;
    enemy_speed = 3;
    frame_left = 0;
    frame_right = 0;
    death_frame_x = 390;
    death_frame_y = 0;

    following_player = false;
    bullet_fire_rate = 0;

    animation_speed_left = 0;
    animation_speed_right = 0;

    sprite_info.w = 63;
    sprite_info.h = 74;
    sprite_info.y = spawn_point_y;
    enemy_current_floor_y = spawn_point_y;

    on_block = false;
}

void Enemy::Draw_enemy_going_right()
{
    moving_right = true;
    character_image.drawAnim( sprite_info.x, sprite_info.y, 63, 74, frame_right, 459);
    Move_frame_right();
}

void Enemy::Draw_enemy_going_left()
{
    moving_right = false;
    character_image.drawAnim( sprite_info.x, sprite_info.y, 63, 74, frame_left, 155);
    Move_frame_left();
}

void Enemy::Draw_enemy_standing_right()
{
    moving_right = true;
    character_image.drawAnim( sprite_info.x, sprite_info.y, 63, 74, 0, 459);
}

void Enemy::Draw_enemy_standing_left()
{
    moving_right = false;
    character_image.drawAnim( sprite_info.x, sprite_info.y, 63, 74, 0, 155);
}

void Enemy::Draw_death( int bgX3, int y)
{
    sprite_info.y = y;
    character_image.drawAnim( sprite_info.x, sprite_info.y, 63, 74, death_frame_x, death_frame_y);

    if( death_frame_x < 600 )
    {
        death_frame_x += 70;
    }
    else if( death_frame_y < 82 )
    {
        death_frame_y += 82;
        death_frame_x = 390;
    }
    // this is the case to display enemy remains on screen after
    // the death animation
    else
    {
        sprite_info.x = bgX3 + spawn_x;
        character_image.drawAnim( sprite_info.x, sprite_info.y, 63, 74, death_frame_x, death_frame_y);
    }

} // Draw_death()

void Enemy::Move_frame_left()
{
    if( 3 == animation_speed_left )
    {
         if (frame_left < 252)
        {
            frame_left=frame_left+63;
        }
        else
            frame_left=0;

        animation_speed_left = 0;
    }
    else
        animation_speed_left++;
}
//
void Enemy::Move_frame_right()
{
    if( 3 == animation_speed_right )
    {
         if (frame_right < 252)
        {
            frame_right=frame_right+63;
        }
        else
            frame_right=0;

        animation_speed_right = 0;
    }
    else
        animation_speed_right++;
}

void Enemy::Draw_me( int bgX3, int y, int player_level_x, int player_screen_x  )
{
    //sprite_info.y = y;
    sprite_info.x = bgX3 + spawn_x;

    // if the player is detected by an enemy
    if( player_level_x > (sprite_info.x - distance_to_enemy)
        && player_level_x < (sprite_info.x + distance_to_enemy) )
    {
        following_player = true;
    }

    if( false == on_block )
    {
        if( false == jumped )
        {
            if( sprite_info.y < enemy_current_floor_y )
            {
                sprite_info.y += 3;
            }
            else
            {
                //sprite_info.y = enemy_current_floor_y;
            }
        }
    }

    // enemy needs to follow the player
    if( following_player )
    {
        Fire_bullet();

        if( player_screen_x >= sprite_info.x - 190 && player_screen_x <= (sprite_info.x + sprite_info.w) + 190 )
        {
            // check to see if enemy needs to be turned around
            if( player_screen_x < sprite_info.x ) // - 50 // >
            {
                Draw_enemy_standing_left();
            }
            else
            {
                Draw_enemy_standing_right();
            }
        }
        // else just move the enemy in the correct direction.
        else if( player_screen_x > sprite_info.x )
        {

            if( true == moving_right )
            {
                spawn_x += enemy_speed;
            }
            Draw_enemy_going_right();

        }
        else if( player_screen_x < sprite_info.x )
        {

            if( false == moving_right )
            {
                spawn_x -= enemy_speed;
            }
            Draw_enemy_going_left();

        }

    } // following player

} // Draw_me()

void Enemy::Fire_bullet()
{
    bullet_fire_rate++;
    if( 0 == bullet_fire_rate % 70 )
    {
        Set_attacking( true );
    }
    else
    {
        Set_attacking( false );
    }

}

void Enemy::Draw( int bgX3, int y, int player_level_x, int player_screen_x )
{
    if( !dead )
        Draw_me( bgX3, y, player_level_x, player_screen_x );
    else
        Draw_death( bgX3, y);

}

bool Enemy::Following_player()
{
    return following_player;
}

void Enemy::Jump()
{
    jumped = true;
    sprite_info.y -= 3;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------- CLASS ENEMY_CONTROL -------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////////////////


Enemy_control::Enemy_control()
{

}

Enemy_control::~Enemy_control()
{
   // Free_list_from_memory();
}

void Enemy_control::Add_enemy( Enemy a_enemy )
{
    enemy_vector.push_back( a_enemy );
}

void Enemy_control::Draw( int bgX3, int y, int player_level_x, int player_screen_x )
{
    //cout << "enemy vector size: " << enemy_vector.size() << endl;
    for( int c = 0; c < enemy_vector.size(); c++ )
    {
        enemy_vector.at(c).Draw( bgX3, y, player_level_x, player_screen_x );
    }
    //cout << "enemy vector size: " << enemy_vector.size() << endl;
}

//vector< Enemy >::iterator Enemy_control::Get_enemy_control_iter()
//{
//    return enemy_control_iter;
//}

vector< Enemy >::iterator Enemy_control::Get_enemy_control_begin()
{
    return enemy_vector.begin();
}

vector< Enemy >::iterator Enemy_control::Get_enemy_control_end()
{
    //enemy_control_iter = enemy_vector.end();
    return enemy_vector.end();
}
